package com.ilop.sthome.utils.greenDao.utils;

import com.ilop.sthome.data.greenDao.UserInfoBean;
import com.ilop.sthome.data.greenDao.UserInfoBeanDao;
import com.ilop.sthome.utils.greenDao.manage.CommonDaoUtils;
import com.ilop.sthome.utils.greenDao.manage.DaoManager;

import java.util.List;

public class UserInfoDaoUtil {

    private volatile static UserInfoDaoUtil instance = null;
    private CommonDaoUtils<UserInfoBean> mUserInfoUtils;

    public static UserInfoDaoUtil getInstance(){
        if (instance == null){
            synchronized (UserInfoDaoUtil.class){
                if (instance == null){
                    instance = new UserInfoDaoUtil();
                }
            }
        }
        return instance;
    }

    public static void setInstance(){
        instance = null;
    }

    private UserInfoDaoUtil() {
        DaoManager mManager = DaoManager.getInstance();
        UserInfoBeanDao mUserInfoDao= mManager.getDaoSession().getUserInfoBeanDao();
        mUserInfoUtils = new CommonDaoUtils(UserInfoBean.class, mUserInfoDao);
    }

    public CommonDaoUtils<UserInfoBean> getUserInfoDao(){
        return mUserInfoUtils;
    }

    public void toUpdateUser(List<UserInfoBean> userInfoList){
        if (mUserInfoUtils.queryById(1) == null){
            mUserInfoUtils.insert(userInfoList.get(0));
        }else {
            userInfoList.get(0).setId((long)1);
            mUserInfoUtils.update(userInfoList.get(0));
        }
    }

    public UserInfoBean findCurrentUser(){
        List<UserInfoBean> user = mUserInfoUtils.queryAll();
        if (user.size()>0){
            return user.get(0);
        }
        return new UserInfoBean();
    }
}
